package com.lkj.youmeng.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.lkj.youmeng.R;
import com.lkj.youmeng.mvp.view.MergeImageView;

import java.util.ArrayList;

/**
 * @作者：JAY
 * @创建时间：2020/3/3 13:44
 * @描述：
 **/
public class BannerAdapter extends PagerAdapter {
    private ArrayList<MergeImageView> imageViewList = new ArrayList<>();
    public BannerAdapter(ArrayList<MergeImageView> imageViewList) {
        this.imageViewList = imageViewList;
    }

    @NonNull
    @Override
    public Object instantiateItem(@NonNull ViewGroup container, int position) {
        if (imageViewList.size()>0){
            int newPosition = position % imageViewList.size();
            if (imageViewList.get(newPosition).getParent() != null){
                ((ViewGroup)imageViewList.get(newPosition).getParent()).removeView(imageViewList.get(newPosition));
            }
            MergeImageView imageView = imageViewList.get(newPosition);
            // a. 把View对象添加到container中
            container.addView(imageView);
            // b. 把View对象返回给框架, 适配器
            return imageView;
        }else {
            return container;
        }
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
//        container.removeView((View)object);
    }

    @Override
    public int getCount() {
        return Integer.MAX_VALUE;
    }

    /**
     * 判断是否使用缓存, 如果返回的是true, 使用缓存. 不去调用instantiateItem方法创建一个新的对象
     */
    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }

    @Override
    public int getItemPosition(@NonNull Object object) {
        return POSITION_NONE;
    }
}
